/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.data;

import java.sql.*;

/**
 *
 * @author Guesmi
 */

public class Responsable extends ITable {

    private static final String Table = "Responsable";

    private int RES_ID ;
    private int UTL_ID ;
    private int STR_ID ;
    private int CAT_ID ;

    /*Functions*/
    public void Responsable(){}

    public int GetIDResponsable(){return this.RES_ID ;}
    
    public User GetIDUtilisateur(){
        return User.SearchByID(this.UTL_ID) ;
        }
    public Structure GetIDStructure(){
        return Structure.SearchByID(this.STR_ID) ;
    }
    public Categorie GetIDCategorie(){
        return Categorie.SearchByID(this.CAT_ID) ;
    }

    public void SetIDResponsable(int res){
        if(Responsable.SearchByID(res)!=null){
        this.RES_ID = res ;}
    }
    public void SetIDResponsable(Responsable res){
        SetIDResponsable(res.GetIDResponsable());
    }
    public void SetIDUtilisateur(int utl){
        if(User.SearchByID(utl)!=null){
        this.UTL_ID = utl ;}
        }
    public void SetIDUtilisateur(User u){
        SetIDUtilisateur(u.getID());
    }
    public void SetIDStructure(int str){
        if(Structure.SearchByID(str)!=null){
        this.STR_ID = str ;}
    }
    public void SetIDStructure(Structure s){
        SetIDStructure(s.GetIDStructure());
    }
    public void SetIDCategorie(int cat){
        if(Categorie.SearchByID(cat)!=null){
        this.CAT_ID = cat ;}
    }
    public void SetIDCategorie(Categorie c){
        SetIDCategorie(c.GetCodeGategorie());
    }

    public void LoadFromDB (String MAT){
        //ConnexionBD db = new ConnexionBD();
        try{
        ResultSet res = db.executerSelect(" Select * from "+ Responsable.Table +" where UTL_MAT ='" + MAT + "'");
        if(res.next())
          {
            this.RES_ID = res.getInt("RES_ID");
            this.UTL_ID = res.getInt("UTL_ID");
            this.STR_ID = res.getInt("STR_ID");
            this.CAT_ID = res.getInt("CAT_ID");
          }
        }catch(Exception sqle){}
        //catch(Exception e){}
        }


    public void SaveToDB() throws Exception{

                db.InsertNewLine(Responsable.Table ,
                    "RES_ID",Integer.toString(this.RES_ID),
                    "UTL_ID",Integer.toString(this.UTL_ID),
                    "STR_ID",Integer.toString(this.STR_ID),
                    "CAT_ID",Integer.toString(this.CAT_ID));
         }

    /************************************************************************
    *                     STATIC FUNCTIONS
    *************************************************************************/

    public static Responsable SearchByID(int ID){
            
            Responsable resp = new Responsable();
            ConnexionBD db = new ConnexionBD ();
            try{
             ResultSet res = db.executerSelect("select * from "+ Responsable.Table +" where RES_ID ='"+ ID +"'");
             while (res.next())
                 {
                 resp.RES_ID = res.getInt("RES_ID");
                 resp.UTL_ID = res.getInt("UTL_ID");
                 resp.STR_ID = res.getInt("STR_ID");
                 resp.CAT_ID = res.getInt("CAT_ID");
                 }
            }catch(Exception sqle){resp = null ;}
            
            return resp ;
    }
      
}